package com.durian.ui.textview;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.collection.SimpleArrayMap;
import android.text.TextUtils;
import android.util.AttributeSet;
import com.durian.base.R;
import com.durian.ui.factory.SelectorFactory;
import java.util.ArrayList;

/**
 * 说明：RoundButton
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019-06-18 14:48
 * <p/>
 * 版本：version 1.0
 */
public class RoundButton extends AppCompatTextView {
    private int leftTopCorner,rightTopCorner,leftBottomCorner,rightBottomCorner;
    private int cornerRadius,strokeWidth;
    private ColorStateList pressedColor,disEnableColor,solidColor,textColor,textPressedColor;
    private ColorStateList                                   strokeColor;
    private boolean                                          isCircle;
    private String                                           gradientColor;
    private SelectorFactory.OnCreateGradientDrawableCallback callback;
    private int rbGradientDirection = 0;

    private static SimpleArrayMap<Integer,GradientDrawable.Orientation> gradimentMap = null;

    public RoundButton(Context context) {
        this(context,null);
    }

    public RoundButton(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (gradimentMap == null){
            gradimentMap = new SimpleArrayMap<>();
            gradimentMap.put(0,GradientDrawable.Orientation.TOP_BOTTOM);
            gradimentMap.put(1,GradientDrawable.Orientation.TR_BL);
            gradimentMap.put(2,GradientDrawable.Orientation.RIGHT_LEFT);
            gradimentMap.put(3,GradientDrawable.Orientation.BR_TL);
            gradimentMap.put(4,GradientDrawable.Orientation.BOTTOM_TOP);
            gradimentMap.put(5,GradientDrawable.Orientation.BL_TR);
            gradimentMap.put(6,GradientDrawable.Orientation.LEFT_RIGHT);
            gradimentMap.put(7,GradientDrawable.Orientation.TL_BR);
        }
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundButton);
        if (array.hasValue(R.styleable.RoundButton_rbGradientColor)){
            gradientColor = array.getString(R.styleable.RoundButton_rbGradientColor);
            if (array.hasValue(R.styleable.RoundButton_rbGradientDirection)){
                rbGradientDirection = array.getInt(R.styleable.RoundButton_rbGradientDirection,0);
                this.callback = drawable -> drawable.setOrientation(gradimentMap.get(rbGradientDirection));
            }
        }
        isCircle = array.getBoolean(R.styleable.RoundButton_rbCircle,false);
        if (array.hasValue(R.styleable.RoundButton_rbPressedColor)) {
            pressedColor = array.getColorStateList(R.styleable.RoundButton_rbPressedColor);
        }
        disEnableColor = array.getColorStateList(R.styleable.RoundButton_rbDisEnableColor);
        solidColor = array.getColorStateList(R.styleable.RoundButton_rbSolidColor);
        textColor = array.getColorStateList(R.styleable.RoundButton_rbTextColor);
        textPressedColor = array.getColorStateList(R.styleable.RoundButton_rbTextPressedColor);
        cornerRadius = array.getDimensionPixelSize(R.styleable.RoundButton_rbCornerRadius,0);
        strokeColor = array.getColorStateList(R.styleable.RoundButton_rbStrokeColor);
        strokeWidth = array.getDimensionPixelSize(R.styleable.RoundButton_rbStrokeWidth,0);
        //        圆角
        leftTopCorner = array.getDimensionPixelSize(R.styleable.RoundButton_rbLeftTopCorner,0);
        rightTopCorner = array.getDimensionPixelSize(R.styleable.RoundButton_rbRightTopCorner,0);
        leftBottomCorner = array.getDimensionPixelSize(R.styleable.RoundButton_rbLeftBottomCorner,0);
        rightBottomCorner = array.getDimensionPixelSize(R.styleable.RoundButton_rbRightBottomCorner,0);

        array.recycle();

        refresh();
    }

    /**
     * 设置边线颜色
     * @param color
     * @return
     */
    public RoundButton setStrokeColor(int color){
        strokeColor = ColorStateList.valueOf(color);
        refresh();
        return this;
    }

    /**
     * 设置边线颜色
     * @param width
     * @return
     */
    public RoundButton setStrokeWidth(int width){
        strokeWidth = width;
        refresh();
        return this;
    }

    /**
     * 设置按压颜色
     * @param color
     * @return
     */
    public RoundButton setPressedColor(int color){
        pressedColor = ColorStateList.valueOf(color);
        refresh();
        return this;
    }

    public RoundButton setCornerRadius(int radius){
        cornerRadius = radius;
        refresh();
        return this;
    }

    /**
     * 设置背景颜色
     * @param color
     * @return
     */
    public RoundButton setSolidColor(int color){
        solidColor = ColorStateList.valueOf(color);
        refresh();
        return this;
    }

    /**
     * 设置文字颜色
     * @param rbTextColor
     * @return
     */
    public RoundButton setRbTextColor(int rbTextColor){
        textColor = ColorStateList.valueOf(rbTextColor);
        refresh();
        return this;
    }

    /**
     * 设置四个角的度数
     * @param leftTop
     * @param rightTop
     * @param rightBottom
     * @param leftBottom
     * @return
     */
    public RoundButton setCorner(int leftTop,int rightTop,int rightBottom,int leftBottom){
        leftTopCorner = leftTop;
        leftBottomCorner = leftBottom;
        rightTopCorner = rightTop;
        rightBottomCorner = rightBottom;
        refresh();
        return this;
    }

    public void refresh(SelectorFactory.OnCreateGradientDrawableCallback callback){
        this.callback = callback;
        refresh();
    }

    private void refresh(){
        SelectorFactory.ColorSelector colorSelector = SelectorFactory.newColorSelector();
        if (textColor != null){
            colorSelector.setDefaultColor(textColor.getDefaultColor());
        }
        if (textPressedColor != null){
            colorSelector.setPressedColor(textPressedColor.getDefaultColor());
        }
        setTextColor(colorSelector.create());

        SelectorFactory.ShapeSelector shapeSelector = SelectorFactory.newShapeSelector();
        StateListDrawable drawable = shapeSelector.create();
        if (isCircle){
            RectF rectF = new RectF(drawable.getBounds());
            shapeSelector.setCornerRadius((int) ((rectF.height() > rectF.width() ? rectF.width() : rectF.height())) / 2);
            shapeSelector.setShape(GradientDrawable.OVAL);
        }else {
            shapeSelector.setShape(GradientDrawable.RECTANGLE);
            shapeSelector.setCornerRadius(cornerRadius);
            shapeSelector.setCornerRadius(leftTopCorner,rightTopCorner,rightBottomCorner,leftBottomCorner);
        }
        shapeSelector.setDefaultBgColor(solidColor != null ? solidColor.getDefaultColor() : Color.parseColor("#00000000"));
        if (pressedColor != null){
            shapeSelector.setPressedBgColor(pressedColor.getDefaultColor());
        }
        if (disEnableColor != null){
            shapeSelector.setDisabledBgColor(disEnableColor.getDefaultColor());
        }
        if (strokeColor != null){
            shapeSelector.setDefaultStrokeColor(strokeColor.getDefaultColor());
        }
        shapeSelector.setCallback(callback);
        int []gradientColor = getGradientColor();
        if (gradientColor != null){
            shapeSelector.setColors(getGradientColor());
        }
        shapeSelector.setStrokeWidth(strokeWidth);
        setBackgroundDrawable(shapeSelector.create());
    }

    @Deprecated
    public void setGradientColor(String colors,SelectorFactory.OnCreateGradientDrawableCallback callback){
        this.gradientColor = colors;
        refresh(callback);
    }

    public void setGradientColor(String colors,GradientDrawable.Orientation direction){
        this.gradientColor = colors;
        this.callback = drawable -> drawable.setOrientation(direction);
        refresh();
    }

    private int[] getGradientColor(){
        int []colors = null;
        if (!TextUtils.isEmpty(gradientColor)){
            String []srcColor = gradientColor.split("#");
            ArrayList<Integer> colorList = new ArrayList<>();
            for (String s:srcColor){
                if (!TextUtils.isEmpty(s)){
                    colorList.add(parseColor("#"+s));
                }
            }
            if (colorList.size() >= 2){
                colors = new int[colorList.size()];
                for (int i = 0;i < colorList.size();i++){
                    colors[i] = colorList.get(i).intValue();
                }
                return colors;
            }else {
                return null;
            }
        }else {
            return null;
        }
    }

    /**
     * 设置背景 点击 不能点击
     * @param pressedColor
     * @param disEnableColor
     * @param solidColor
     */
    public void refreshDrawable(int pressedColor,int disEnableColor,int solidColor){
        SelectorFactory.ShapeSelector shapeSelector = SelectorFactory.newShapeSelector();
        StateListDrawable drawable = shapeSelector.create();
        if (isCircle){
            RectF rectF = new RectF(drawable.getBounds());
            shapeSelector.setCornerRadius((int) ((rectF.height() > rectF.width() ? rectF.width() : rectF.height())) / 2);
            shapeSelector.setShape(GradientDrawable.OVAL);
        }else {
            shapeSelector.setShape(GradientDrawable.RECTANGLE);
            shapeSelector.setCornerRadius(cornerRadius);
            shapeSelector.setCornerRadius(leftTopCorner,rightTopCorner,rightBottomCorner,leftBottomCorner);
        }
        shapeSelector.setDefaultBgColor(solidColor);
        shapeSelector.setPressedBgColor(pressedColor);
        shapeSelector.setDisabledBgColor(disEnableColor);
        shapeSelector.setStrokeWidth(strokeWidth);
        setBackgroundDrawable(shapeSelector.create());
    }

    /**
     * 转换颜色
     * @param src
     * @return
     */
    private int parseColor(String src){
        int color = Color.WHITE;
        try {
            color = Color.parseColor(src);
        }catch (Exception e){
            e.printStackTrace();
        }
        return color;
    }
}
